//
//  RootMissionToDo.m
//  Trading365
//
//  Created by yifei tong on 11-12-14.
//  Copyright (c) 2011年 dhc. All rights reserved.
//

#import "RootMissionToDo.h"
#import "MissionTodoCell.h"
#import "InfoType.h"
#import "MissionList.h"
#import "ApproveList.h"
#import "Session.h"
#import "AppDelegate.h"

@implementation RootMissionToDo
@synthesize table;
@synthesize InfoListTitle,InfoList2Title,InfoSectionTitle;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [InfoListTitle release];
    [InfoList2Title release];
    [InfoSectionTitle release];
    [table release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
//    [self.tabBarController setSelectedIndex:2];
    [super viewDidLoad];
        
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate setMissionTodoViewCtl:self];

    self.title = INFO_STRING_MISSION;
    [self.view setBackgroundColor:BACKGROUND_COLOR];
    //设置导航栏背景颜色
    self.navigationController.navigationBar.tintColor = NAVIGATION_BAR_COLOR;
    UIImage* imageBar = [UIImage imageNamed:PIC_MAIL_TOP_BAR];
    [self.navigationController setNavigationBarHidden:NO];

    NSString* devVersion = [[UIDevice currentDevice] systemVersion];
    if ([devVersion compare:@"4.3.3"] == 0)
    {
        [self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)];
        [self.navigationController.navigationBar setTintColor:NAVIGATION_BAR_COLOR];
    }else
    {
        [self.navigationController.navigationBar setBackgroundImage:imageBar forBarMetrics:UIBarMetricsDefault];
    }
    
    NSArray *title_array = [[NSArray alloc] initWithObjects:INFO_STRING_MISSION_TYPE1,INFO_STRING_MISSION_TYPE2,INFO_STRING_MISSION_TYPE3,INFO_STRING_MISSION_TYPE4,nil];
    self.InfoListTitle = title_array; 
    [title_array release];

    NSArray *title2_array = [[NSArray alloc] initWithObjects:INFO_STRING_MISSION_TYPE5,nil];
    self.InfoList2Title = title2_array; 
    [title2_array release];

    NSArray *section_array = [[NSArray alloc] initWithObjects:INFO_STRING_MISSION_SECTION_TYPE1,INFO_STRING_MISSION_SECTION_TYPE2,nil];
    self.InfoSectionTitle = section_array; 
    [section_array release];

    MissionType1Count = 0;
    MissionType2Count = 0;
    MissionType3Count = 0;
    MissionType4Count = 0;
    MissionType5Count = 0;
    
    if ([[Session share] isLogined] == YES){
//        [self refreshData];
        MissionType1Count = [[Session share] MsgCount1];
        MissionType2Count = [[Session share] MsgCount2];
        MissionType3Count = [[Session share] MsgCount3];
        MissionType4Count = [[Session share] MsgCount4];
        MissionType5Count = [[Session share] MsgCount5];
    }
    
    [table reloadData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void) viewWillAppear:(BOOL)animated
{
    if ([[Session share] isLogined] == YES){
        [self refreshData];
    }
    
   [table reloadData];
}
- (void)viewDidAppear:(BOOL)animated
{

}
//-(void) viewDidAppear:(BOOL)animated
//{
//    if ([[Session share] isLogined] == YES){
//        [self refreshData];
//    }
//    [table reloadData];
//    
//}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - table view

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [InfoSectionTitle count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    return [InfoSectionTitle objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSInteger num=0;
    if (section == 0)
        num=[self.InfoList2Title count];
    if (section == 1)
        num=[self.InfoListTitle count];
    
    return num;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MissionTodoCell";
    
    MissionTodoCell* cell = (MissionTodoCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"MissionTodoCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;
    
    NSString* text = @"";
    
    if (section == 0){
        if (row < [self.InfoList2Title count]) 
        {
            text = [self.InfoList2Title objectAtIndex:row];
        }
        
        cell.Name.text = text;
        cell.Name.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        
        if (row == 0)
            cell.Num.text = [NSString stringWithFormat:@"%d",MissionType5Count];
    }
    
    if (section == 1){
        if (row < [self.InfoListTitle count]) 
        {
            text = [self.InfoListTitle objectAtIndex:row];
        }

        cell.Name.text = text;
        cell.Name.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        
        if (row == 0)
            cell.Num.text = [NSString stringWithFormat:@"%d",MissionType1Count];
        if (row == 1)
            cell.Num.text = [NSString stringWithFormat:@"%d",MissionType2Count];
        if (row == 2)
            cell.Num.text = [NSString stringWithFormat:@"%d",MissionType3Count];
        if (row == 3)
            cell.Num.text = [NSString stringWithFormat:@"%d",MissionType4Count];
    }
    

    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int section=indexPath.section;
    
    if (section == 0){
        ApproveList *approvelist = [[ApproveList alloc] init];
        [approvelist setMsgType:[NSString stringWithFormat:@"0%d",indexPath.row+1]];
   
        NSString* title = @"";
        if (indexPath.row < [self.InfoList2Title count]) 
        {
            title = [self.InfoList2Title objectAtIndex:indexPath.row];
        }
        [approvelist setMissionTitle:title];
        [self.navigationController pushViewController: approvelist animated:YES];
        [approvelist release];
    }else{
        MissionList *missionlist = [[MissionList alloc] init];
        [missionlist setMsgType:[NSString stringWithFormat:@"0%d",indexPath.row+1]];
        
        NSString* title = @"";
        if (indexPath.row < [self.InfoListTitle count]) 
        {
            title = [self.InfoListTitle objectAtIndex:indexPath.row];
        }
        [missionlist setMissionTitle:title];
        [self.navigationController pushViewController: missionlist animated:YES];
        [missionlist release];
    }
}

#pragma mark - UserTask

-(void) OnPreExcute
{
//    [super OnPreExcute];
}

-(void) OnPostExcute
{    
//    [super OnPostExcute];
}

-(void) DoInBackground
{
    NSMutableDictionary *tmp=nil;
    XmlRpc *xml = [[XmlRpc alloc] init];
    
    tmp = [xml GetMsgCount:@"" PosiID:[[Session share] getMemberPosiID] Type:@"00"];
    MissionType1Count = [[tmp objectForKey:@"count1"] intValue];
    MissionType2Count = [[tmp objectForKey:@"count2"] intValue];
    MissionType3Count = [[tmp objectForKey:@"count3"] intValue];
    MissionType4Count = [[tmp objectForKey:@"count4"] intValue];
    
    [xml release];
    
    [self.table reloadData];
}

#pragma  mark - myfun

-(void)refreshData
{    
//    [[[TaskManager Share] GetTaskByType:GetMissionTodoList Delegate:self] Excute];
    MissionType1Count = [[Session share] MsgCount1];
    MissionType2Count = [[Session share] MsgCount2];
    MissionType3Count = [[Session share] MsgCount3];
    MissionType4Count = [[Session share] MsgCount4];
    MissionType5Count = [[Session share] MsgCount5];

    [table reloadData];
}

@end
